<?php

namespace App\Jobs\goods;

use App\Models\goods\GoodsView;
use App\Models\goods\OrderGroup;
use App\Models\log\ErrorLog;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class GroupQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数。
     *
     * @var int
     */
    public $tries = 3;


    protected $group_id;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($group_id)
    {
        $this->group_id = $group_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $group = OrderGroup::find($this->group_id);
            if ($group->total >= $group->num){
                $map['order_group_id'] = $group->id;
                $map['status'] = 1;
            }


        }catch (\Exception $e){
            dump($e->getMessage());
            $log['msg']     = $e->getMessage();
            $log['params']  = $this->group_id;
            $log['content'] = $e;
            ErrorLog::create($log);
        }
    }


    public function failed(\Exception $exception)
    {
        dump($exception->getMessage());
        $log['msg']     = $exception->getMessage();
        $log['params']  = $this->group_id;
        $log['content'] = $exception;
        ErrorLog::create($log);
    }
}
